#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include "array.h"

const BLOCK_SIZE=20;

Array array_create(int init_size)
{
	Array a;
	a.size=init_size;
	a.array=(int*)malloc(sizeof(int)*a.size);
	return a; 
}

void array_free(Array *a)
{
	free(a->array);
	a->array=NULL;
	a->size=0;
}

int array_size(const Array *a)
{
	return a->size;
}

int array_get(const Array *a,int index)
{
	return a->array[index];
}

int array_set(Array *a,int index,int value)
{
	a->array[index]	= value;
}

int* array_at(Array *a,int index)
{
	printf("array_at size=%d,index=%d\n",a->size,index);
	if(index>=a->size){
		array_inflate(a,(index/BLOCK_SIZE+1)*BLOCK_SIZE-a->size);
	}
	//printf("a->array[%d]=%x\n",index,a->array[index]);
	return &(a->array[index]);
}

void array_inflate(Array *a,int more_size)
{
	int *p=(int*)malloc(sizeof(int)*(a->size+more_size));
	int i;
	for(i=0;i<a->size;i++){
		p[i]=a->array[i];
		printf("p[%d]=%d\t",i,p[i]);
	}

	printf("\n");
	free(a->array);
	a->array=p;
	a->size+=more_size;
	printf("ap->array=%x\n",a->array);
	printf("ap->array=%x\n",a->array[0]);
	printf("ap->array=%x\n",a->array[1]);
	printf("p=%x\n",p);
	printf("&p[0]=%x\n",&p[0]);
	printf("p[0]=%x\n",p[0]);
	printf("p[1]=%x\n",p[1]);
	printf("size=%d\n",a->size);
}


int main(int argc,char const *argv[]) 
{	
	Array a=array_create(3);
	
	printf("%d\n",array_size(&a));
	printf("%d\n",a.size);
	//*array_at(&a,0)=10;
	//printf("%d\n",*array_at(&a,0));
	
	int number;
	int cnt=0;
	while(1){
		scanf("%d",&number);
		if(number!=-1){
			*array_at(&a,cnt++)=number;
		}
	}
	
	array_free(&a);
	
	return 0;
}




